package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.Role;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 查询所有的角色
     *
     * @return
     */
    @RequestMapping("/findAllRoles")
    public Result findAllRoles() {

        List<Role> list = null;
        try {
            list = roleService.findAllRoles();
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public Result insertRole(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.insertRole(role, permissionIds, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            //提示错误信息
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        //提示成功消息
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    /* *
     * 查询单项角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        Role role = roleService.findRoleById(id);

        if (role == null) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, role);
        }
    }

    /**
     * 查询角色对应的权限ids
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIds")
    public List<Integer> findPermissionIds(Integer id) {
        return roleService.findPermissionIds(id);
    }

    /**
     * 查询角色对应的菜单ids
     * @param id
     * @return
     */
    @RequestMapping("/findMenuIds")
    public List<Integer> findMenuIds(Integer id) {
        return roleService.findMenuIds(id);
    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findRoleByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findRoleByPage(queryPageBean);
        return pageResult;
    }


    /**
     *
     * 删除角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result deleteRole(Integer id) {
        try {
            roleService.deleteRole(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        //删除成功
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 修改角色
     *
     * @return
     */
    @RequestMapping("/update")
    public Result updateRole(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.updateRole(role,permissionIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑 修改失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        //编辑成功
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

}
